package com.lagou.homework.utls;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 工具类的创建
 */

public class DbUtil {


    private static String jdbcName;
    private static String dbUrl;
    private static String dbUserName;
    private static String dbpassword;

    //使用静态代码块进行初始化
    static {
        jdbcName="com.mysql.jdbc.Driver";
        dbUrl="jdbc:mysql://localhost:3306/db_web";
        dbUserName="root";
        dbpassword="112233";
        try {
            Class.forName(jdbcName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
/**
 * 获取连接
 */
    public static Connection getConnection() throws SQLException {
        Connection con = DriverManager.getConnection(dbUrl,dbUserName,dbpassword);
        return con;
    }
    //关闭资源
    public static void closeResource(Connection con, PreparedStatement ps) throws SQLException {
        if(null!=con){
            con.close();
        }
        if(null!=ps){
            ps.close();
        }

    }





}
